/*
 * Created by IntelliJ IDEA.
 * User: amrk
 * Date: 14/02/2004
 * Time: 18:36:50
 */
package com.talios.jira.settings;

import com.intellij.openapi.project.Project;
import com.talios.jira.browser.signals.JiraServerDetails;
import com.talios.jira.idea.JiraConfiguration;
import com.talios.jira.idea.JiraConfigurationComponent;

import javax.swing.table.AbstractTableModel;
import java.util.List;

public class ServersTableModel extends AbstractTableModel {

    private JiraConfiguration config;
    private Project project;
    private List serverList;

    public ServersTableModel(Project project) {
        this.project = project;
        config = project.getComponent(JiraConfigurationComponent.class).getConfiguration();

        serverList = config.getServerList();
    }

    public int getColumnCount() {
        return 2;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Base URL";
            case 1:
                return "User Name";
            default:
                return "";
        }
    }

    public int getRowCount() {
        return serverList.size();
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        JiraServerDetails serverDetails = getServer(rowIndex);
        if (serverDetails == null) {
            return "ERR";
        }

        switch (columnIndex) {
            case 0:
                return serverDetails.getBaseurl();
            case 1:
                return serverDetails.getUsername();
            default:
                return "";
        }
    }

    public JiraServerDetails getServer(int selectedRow) {
        if (selectedRow != -1) {
            JiraServerDetails serverDetails = (JiraServerDetails) serverList.get(selectedRow);
            return serverDetails;
        } else {
            return null;
        }
    }
}